//
//  MainViewController.m
//  SGTopTitleViewExample
//
//  Created by Sorgle on 16/8/24.
//  Copyright © 2016年 Sorgle. All rights reserved.
//

#import "HomeViewController.h"
#import "SGNavigationItemTitleView.h"
#import "SGTopTitleView.h"
#import "RecommendViewController.h"
#import "DomesticViewController.h"
#import "InternationalViewController.h"
#import "BeautyViewController.h"
#import "EntertainmentViewController.h"
#import "ConstellationControlViewController.h"
#import "LaughViewController.h"
#import "PhotoViewController.h"
#import "SatinViewController.h"
#import "SportViewController.h"
#import "UpKnowledgeViewController.h"
#import "AFNetworking.h"
#import "Model.h"
#import "YMJMacro_h.h"
#import "UIScrollView+JHRefresh.h"
#import "JHRefreshAmazingAniView.h"
@interface HomeViewController ()<SGTopTitleViewDelegate, UIScrollViewDelegate>
@property (nonatomic, strong) SGTopTitleView *topTitleView;
@property (nonatomic, strong) NSArray *titles;
@end

@implementation HomeViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.edgesForExtendedLayout = 0;
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.leftBarButtonItems = nil;
    self.navigationItem.rightBarButtonItems = nil;
    
    // 1.添加所有子控制器
    [self setupChildViewController];
    
    
    self.titles = @[@"推荐", @"国内", @"国际", @"美女", @"娱乐", @"写真", @"段子", @"笑点低", @"星座控",@"涨知识",@"体育圈"];
    self.topTitleView = [SGTopTitleView topTitleViewWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    _topTitleView.scrollTitleArr = [NSArray arrayWithArray:_titles];
    _topTitleView.delegate_SG = self;
    
    // 对navigationItem.titleView的包装，为的是让View占据整个视图宽度
    SGNavigationItemTitleView *view = [[SGNavigationItemTitleView alloc] init];
    view.backgroundColor = [UIColor clearColor];
    [view addSubview:_topTitleView];
    self.navigationItem.titleView = view;
    
    // 创建底部滚动视图
    self.mainScrollView = [[UIScrollView alloc] init];
    _mainScrollView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 44);
    _mainScrollView.contentSize = CGSizeMake(self.view.frame.size.width * _titles.count, 0);
    _mainScrollView.backgroundColor = [UIColor clearColor];
    // 开启分页
    _mainScrollView.pagingEnabled = YES;
    // 没有弹簧效果
    _mainScrollView.bounces = NO;
    // 隐藏水平滚动条
    _mainScrollView.showsHorizontalScrollIndicator = NO;
    // 设置代理
    _mainScrollView.delegate = self;
    [self.view addSubview:_mainScrollView];
    
    RecommendViewController *oneVC = [[RecommendViewController alloc] init];
    [self.mainScrollView addSubview:oneVC.view];
    [self addChildViewController:oneVC];


}

#pragma mark - - - SGTopScrollMenu代理方法
- (void)SGTopTitleView:(SGTopTitleView *)topTitleView didSelectTitleAtIndex:(NSInteger)index {
    
    // 1 计算滚动的位置
    CGFloat offsetX = index * self.view.frame.size.width;
    self.mainScrollView.contentOffset = CGPointMake(offsetX, 0);
    
    
    // 2.给对应位置添加对应子控制器
    [self showVc:index];
}

// 添加所有子控制器
- (void)setupChildViewController {
    // 推荐
    RecommendViewController *oneVC = [[RecommendViewController alloc] init];
    [self addChildViewController:oneVC];
    
    // 国内
    DomesticViewController *twoVC = [[DomesticViewController alloc] init];
    [self addChildViewController:twoVC];
    
    // 国际
    InternationalViewController *threeVC = [[InternationalViewController alloc] init];
    [self addChildViewController:threeVC];
    
    // 美女
    BeautyViewController *fourVC = [[BeautyViewController alloc] init];
    [self addChildViewController:fourVC];
    
    // 娱乐
    EntertainmentViewController *fiveVC = [[EntertainmentViewController alloc] init];
    [self addChildViewController:fiveVC];
    
    // 写真
    PhotoViewController *sixVC = [[PhotoViewController alloc] init];
    [self addChildViewController:sixVC];
    
    // 段子
    SatinViewController *sevenVC = [[SatinViewController alloc] init];
    [self addChildViewController:sevenVC];
    
    // 笑点低
    LaughViewController *eightVC = [[LaughViewController alloc] init];
    [self addChildViewController:eightVC];
    
    // 星座控
    ConstellationControlViewController *nineVC = [[ConstellationControlViewController alloc] init];
    [self addChildViewController:nineVC];
    
    // 涨知识
    UpKnowledgeViewController *tenVC = [[UpKnowledgeViewController alloc] init];
    [self addChildViewController:tenVC];
    
    // 体育圈
    SportViewController *elevenVC = [[SportViewController alloc] init];
    [self addChildViewController:elevenVC];
}

// 显示控制器的view
- (void)showVc:(NSInteger)index {
    
    CGFloat offsetX = index * self.view.frame.size.width;
    
    UIViewController *vc = self.childViewControllers[index];
    
    // 判断控制器的view有没有加载过,如果已经加载过,就不需要加载
    if (vc.isViewLoaded) return;
    
    [self.mainScrollView addSubview:vc.view];
    vc.view.backgroundColor = [UIColor yellowColor];
    vc.view.frame = CGRectMake(offsetX, 0, self.view.frame.size.width, self.view.frame.size.height);
    
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    // 计算滚动到哪一页
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    
    // 1.添加子控制器view
    [self showVc:index];
    
    // 2.把对应的标题选中
    UILabel *selLabel = self.topTitleView.allTitleLabel[index];
    
    
    [self.topTitleView scrollTitleLabelSelecteded:selLabel];
    // 3.让选中的标题居中
    [self.topTitleView scrollTitleLabelSelectededCenter:selLabel];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:0];
    self.navigationController.navigationBar.barStyle = 0;
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];

}

@end


